<template>
  <div class="floating-bot" :class="{ 'floating-bot--hidden': !visible }">
    <div 
      class="floating-bot__button"
      :class="{ 'floating-bot__button--pulse': showPulse }"
      @click="handleClick"
    >
      <!-- 机器人图标 -->
      <div class="floating-bot__icon">
        <svg viewBox="0 0 24 24" fill="currentColor">
          <path d="M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2ZM21 9V7L15 1H5C3.9 1 3 1.9 3 3V7C3 8.1 3.9 9 5 9H8V11C8 12.1 8.9 13 10 13H14C15.1 13 16 12.1 16 11V9H19C20.1 9 21 8.1 21 7V9ZM7.5 6C7.5 6.8 6.8 7.5 6 7.5S4.5 6.8 4.5 6 5.2 4.5 6 4.5 7.5 5.2 7.5 6ZM19.5 6C19.5 6.8 18.8 7.5 18 7.5S16.5 6.8 16.5 6 17.2 4.5 18 4.5 19.5 5.2 19.5 6ZM16 15H8C6.9 15 6 15.9 6 17V21C6 22.1 6.9 23 8 23H16C17.1 23 18 22.1 18 21V17C18 15.9 17.1 15 16 15ZM10 19C10 19.6 9.6 20 9 20S8 19.6 8 20 8.4 19 9 19 10 18.4 10 19ZM15 19C15 19.6 14.6 20 14 20S13 19.6 13 20 13.4 19 14 19 15 18.4 15 19Z"/>
        </svg>
      </div>
      
      <!-- 消息提示点 -->
      <div v-if="hasUnreadMessage" class="floating-bot__badge"></div>
      
      <!-- 悬停提示 -->
      <div class="floating-bot__tooltip">
        AI购物助手
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'

// Props
interface Props {
  visible?: boolean
  hasUnreadMessage?: boolean
  showPulse?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  visible: true,
  hasUnreadMessage: false,
  showPulse: true
})

// Emits
const emit = defineEmits<{
  click: []
}>()

const router = useRouter()

// 响应式数据
const isScrolled = ref(false)

// 方法
function handleClick(): void {
  emit('click')
  // 导航到AI聊天页面
  router.push('/ai-chat')
}

function handleScroll(): void {
  isScrolled.value = window.scrollY > 100
}

// 生命周期
onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
.floating-bot {
  position: fixed;
  bottom: 80px;
  right: 20px;
  z-index: 1000;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.floating-bot--hidden {
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px);
}

.floating-bot__button {
  position: relative;
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

.floating-bot__button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
}

.floating-bot__button:active {
  transform: translateY(0);
}

.floating-bot__button--pulse {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  }
  50% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.8), 0 0 0 10px rgba(102, 126, 234, 0.1);
  }
  100% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  }
}

.floating-bot__icon {
  width: 28px;
  height: 28px;
  color: white;
  transition: transform 0.3s ease;
}

.floating-bot__button:hover .floating-bot__icon {
  transform: scale(1.1);
}

.floating-bot__badge {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 12px;
  height: 12px;
  background: #ff4757;
  border-radius: 50%;
  border: 2px solid white;
  animation: bounce 1s infinite;
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-3px);
  }
  60% {
    transform: translateY(-2px);
  }
}

.floating-bot__tooltip {
  position: absolute;
  right: 70px;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  pointer-events: none;
}

.floating-bot__tooltip::after {
  content: '';
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  border: 6px solid transparent;
  border-left-color: rgba(0, 0, 0, 0.8);
}

.floating-bot__button:hover .floating-bot__tooltip {
  opacity: 1;
  visibility: visible;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .floating-bot {
    bottom: 70px;
    right: 16px;
  }
  
  .floating-bot__button {
    width: 50px;
    height: 50px;
  }
  
  .floating-bot__icon {
    width: 24px;
    height: 24px;
  }
  
  .floating-bot__tooltip {
    display: none;
  }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .floating-bot__button {
    background: linear-gradient(135deg, #4c6ef5 0%, #6c5ce7 100%);
    box-shadow: 0 4px 20px rgba(76, 110, 245, 0.4);
  }
  
  .floating-bot__button:hover {
    box-shadow: 0 8px 25px rgba(76, 110, 245, 0.6);
  }
}
</style>